/**
 * 
 */
package edu.umd.clip.lm.model.decoding;

import edu.umd.clip.lm.model.HistoryTreePayload;
import edu.umd.clip.lm.model.OnDiskCompactProbTree;
import edu.umd.clip.lm.model.decoding.TrellisSlice.SliceItem;
import edu.umd.clip.lm.storage.RequestBundle;
import edu.umd.clip.lm.storage.AbstractProbTreeStorage.Key;
import edu.umd.clip.lm.util.tree.BinaryTree;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class SentenceContext {
	private Trellis trellis;
	private RequestBundle<Key,OnDiskCompactProbTree> currentRequests;
	//protected SliceItem items[];
	//protected LinkQueue[] queues;
	//protected BinaryTree<HistoryTreePayload>[] predictions;
	protected double logScale;
	private double lastSliceTotalProb;
	
	protected SentenceContext(int numPrefixStates) {
		this();
	}
	
	protected SentenceContext() {
		trellis = new Trellis();
		currentRequests = null;
		logScale = 0;
	}
	
	public double getLogProb() {
		double prob = getCurrentSlice().getTotalProb();
		assert(prob > 0);
		assert(!Double.isNaN(logScale));
		return Math.log10(prob) + logScale;
	}
	
	public SentenceContext addWord(long overtFactors) {
		trellis.newSlice(overtFactors);
		return this;
	}

	/**
	 * @return
	 * @see edu.umd.clip.lm.model.decoding.Trellis#getCurrentSlice()
	 */
	public TrellisSlice getCurrentSlice() {
		return trellis.getCurrentSlice();
	}

	/**
	 * @param timeOffset
	 * @return
	 * @see edu.umd.clip.lm.model.decoding.Trellis#getSliceByOffset(int)
	 */
	public TrellisSlice getSliceByOffset(int timeOffset) {
		return trellis.getSliceByOffset(timeOffset);
	}

	/**
	 * @return the trellis
	 */
	public Trellis getTrellis() {
		return trellis;
	}

	public int getDebug() {
		return trellis.getDebug();
	}

	public void setDebug(int debug) {
		trellis.setDebug(debug);
	}

	public RequestBundle<Key,OnDiskCompactProbTree> getCurrentRequests() {
		return currentRequests;
	}

	public void setCurrentRequests(RequestBundle<Key,OnDiskCompactProbTree> currentRequests) {
		this.currentRequests = currentRequests;
	}
	
	protected void addLogScale(double logScale) {
		this.logScale += logScale;
	}
	
	public double getLogScale() {
		return logScale;
	}

	protected double getLastSliceTotalProb() {
		return lastSliceTotalProb;
	}

	protected void setLastSliceTotalProb(double lastSliceTotalProb) {
		this.lastSliceTotalProb = lastSliceTotalProb;
	}
}
